package com.changpay.zywy.ui.fragment.main

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.CountTypeAdapter
import com.changpay.zywy.base.BaseFragment
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.ProjectBean
import com.changpay.zywy.constant.Constant
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.ui.activity.count.CountSelectActivity
import com.changpay.zywy.ui.activity.mine.RenZhenActivity
import com.changpay.zywy.utils.Preference
import kotlinx.android.synthetic.main.fragment_count_type.*

class CountTypeFragment : BaseFragment(), IView {
    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {

    }

    override fun showMsg(msg: String) {

    }

    override fun showError(errorMsg: String) {

    }

    override fun showLoading() {

    }


    /**
     * local password
     */
    private var approve: Int by Preference(Constant.IS_APPROVE, 0)


    private val onClickListener = View.OnClickListener { view ->
        if (approve == 0) {
            showToast("请先实名认证")
            startActivity(RenZhenActivity::class.java)
            return@OnClickListener
        }
        when (view.id) {

        }

    }

    companion object {
        fun getInstance(): CountTypeFragment = CountTypeFragment()
    }


    override fun attachLayoutRes(): Int = R.layout.fragment_count_type

    override fun lazyLoad() {


        val param = hashMapOf<String, String>()
        param["key"] = token
        RetrofitHelper.service.getProject(param).sss(this)
        {
            mAdapter.addData(it.result)
        }
    }

    override fun initView(view: View) {
        initRecyclerView()
    }


    /**
     * datas
     */
    private val datas = mutableListOf<ProjectBean>()


    /**
     * Adapter
     */
    private val mAdapter: CountTypeAdapter by lazy {
        CountTypeAdapter(context, datas)
    }


    /**
     * LinearLayoutManager
     */
    private val GridLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(context)
    }


    /**
     * ItemClickListener
     */
    private val ItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        val intent = Intent(context, CountSelectActivity::class.java)
        intent.putExtra("gc_id",  datas[position].gc_id.toString())
        startActivity(intent)
    }


    private fun initRecyclerView() {
        rv_count_type.run {
            layoutManager = GridLayoutManager
            adapter = mAdapter
//            itemAnimator = DefaultItemAnimator()
//            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }
        mAdapter.run {
            bindToRecyclerView(rv_count_type)
//            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            onItemClickListener = this@CountTypeFragment.ItemClickListener
        }
    }
}